<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>奖品列表</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/base.css"></link>
    <style>
    body {
      font-family: Arial, sans-serif;
      padding: 0 30px;
    }
    table {
      /* height: calc(100vh - 143px); */
      height: 300px;
    }
    .table td, .table th{
      border: none;
    }
    .table thead th{
      background-color: #f7f7f7;
      border-bottom: none;
      font-weight: 600;
      font-size: 16px;
      color: #999999;
      height: 80px;
      line-height: 80px;
      padding: 0;
      position: sticky;
      top: 0px;
    }
    .table tbody+tbody{
      border-top: none;
    }
    .prize-table {
      width: 100%; /* 表格宽度为容器宽度的100% */
      height: 100vh;
      overflow-y: auto;
    }
    .prize-table h2{
      background: #fff;
      width: 100%;
      font-weight: 600;
      font-size: 18px;
      color: #000000;
      height: 70px;
      display: flex;
      align-items: center;
      margin-bottom: 0;
    }
    .table-box{
      height: calc(100vh - 140px);
      overflow-y: auto;
    }
    .prize-table th, .prize-table td {
        text-align: center;
        vertical-align: middle;
        padding: 10px;
        height: 50px; /* 统一设置表头和单元格的高度 */
    }
    /* 第一列和第三列的样式 */
    .prize-table th:first-child, .prize-table td:first-child,
    .prize-table th:nth-child(3), .prize-table td:nth-child(3),
    .prize-table th:nth-child(5), .prize-table td:nth-child(5) {
        width: 15%; /* 设置第一列和第三列的宽度为15% */
    }
    /* 第四列的样式 */
    .prize-table th:nth-child(4), .prize-table td:nth-child(4) {
        width: 30%; /* 设置第四列的宽度为30% */
    }
    /* 其他列的样式（例如第二列和第五列） */
    .prize-table th:nth-child(2), .prize-table td:nth-child(2) {
        width: 25%; /* 保持第二列和第五列的宽度为20% */
    }
    .prize-table th {
      /* 如果有特定的样式只适用于表头，可以在这里添加 */
    }
    .prize-table td {
      height: 100px; /* 特定于单元格的高度设置 */
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 300px;
    }
    .prize-table img{
      width: 76px;
      height: 76px;
      border-radius: 8px;
      object-fit: scale-down;
      border:1px solid #e4e4e4;
    }
    .pagination {
      display: flex;
      justify-content: flex-end;
      margin-top: 18px;
      padding-right: 16px;
    }
    .pagination button {
      margin: 0 5px; /* 按钮之间的间距保持不变 */
      border-radius: 5px; /* 设置圆角为20像素，可以根据需要调整 */
      border: 1px solid #007bff;
      background-color: #fff;
      padding: 0px 8px; /* 可以添加一些内边距，使按钮看起来更饱满 */
      cursor: pointer; /* 将鼠标光标改为指针形状，提升用户体验 */
      font-size: 13px;
    }
    .pagination span{
      margin: 0 10px;
      font-size: 14px;
    }
    .pagination input{
      width: 80px;
      text-align: center;
    }
  </style>
</head>
<body>
<div class=" prize-table">
    <h2>奖品列表</h2>
    <div class="table-box">
      <table class="table">
        <thead>
        <tr>
            <th>奖品id</th>
            <th>奖品图</th>
            <th>奖品名</th>
            <th>奖品描述</th>
            <th>奖品价值</th>
        </tr>
        </thead>
        <tbody>
        <tbody id="prizeList">
            <!-- 奖品列表将动态插入这里 -->
        </tbody>
        </tbody>
      </table>
    </div>
    <div class="pagination">
        <button class="btn-outline-primary" onclick="fetchPrizes(1)">首页</button>
        <button class="btn-outline-primary" onclick="previousPage()">上一页</button>
        <span>第 <input type="number" id="pageInput" min="1" value="1" /> 页</span>
        <button class="btn-outline-primary" onclick="nextPage()">下一页</button>
        <button class="btn-outline-primary" onclick="fetchPrizes(totalPages)">尾页</button>
    </div>
</div>


<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    var userToken = localStorage.getItem("user_token");

    var currentPage = 1;
    var pageSize = 10;
    var totalPages;

    // 发送AJAX请求的函数
    function fetchPrizes(page) {
      console.log(page);
      // 如果页码小于1，则重置为1
      if (page < 1) {
        page = 1;
      }

      // 更新当前页码
      currentPage = page;

      // 构建要发送的数据对象
      var dataToSend = {
        currentPage: currentPage,
        pageSize: pageSize
      };

      // 发送AJAX请求
      $.ajax({
          url: '/prize/find-list',
          type: 'GET',
          data: dataToSend, // 将分页参数作为请求数据发送
          headers: {
              // jwt
              "user_token": userToken
          },
          dataType: 'json', // 期望从服务器接收的数据类型是JSON
          success: function(result) {
            if (result.code != 200) {
                alert("查询奖品列表失败！" + result.msg);
            } else {
                var prizes = result.data.records;
                var prizesHtml = '';
                // 清空现有的表格内容
                var prizeList = $('#prizeList');
                prizeList.empty();
                prizes.forEach(function(prize) {
                    var imageUrl = prize.imageUrl ? prize.imageUrl : '/pic/defaultPrizeImg.png';
                    prizeList.append('<tr> ' +
                            '<td>' + prize.prizeId + '</td>' +
                            '<td><img src="' + imageUrl + '" alt="' + prize.prizeName + '" class="prize-image"></td>' +
                            '<td>' + prize.prizeName + '</td>' +
                            '<td>' + prize.description + '</td>' +
                            '<td>' + prize.price + '元</td>' +
                            '</tr>');
                });
                // 更新分页控件的总页数
                totalPages = Math.ceil(result.data.total / pageSize);
                // 更新输入框的值
                $('#pageInput').val(currentPage);
              } // else end
          },
          error:function(err){
              console.log(err);
              if(err!=null && err.status==401){
                  alert("用户未登录, 即将跳转到登录页!");
                  // 跳转登录页
                  window.location.href = "/blogin.html";
                  window.parent.location.href = "/blogin.html";//让父页面一起跳转
              }
          }
      });
    }

    function previousPage() {
      if (currentPage > 1) {
        fetchPrizes(currentPage - 1);
      } else {
        alert("已经是第一页");
      }
    }

    function nextPage() {
      if (currentPage < totalPages) {
        fetchPrizes(currentPage + 1);
      } else {
        alert("已经是最后一页");
      }
    }

    $(document).ready(function() {
      fetchPrizes(1);
    });

    // 绑定输入框回车事件
    $('#pageInput').on('keypress', function(e) {
      if (e.key === 'Enter') {
        var page = parseInt(this.value, 10);
        if (!isNaN(page) && page >= 1 && page <= totalPages) {
          fetchPrizes(page);
        }
      }
    });

</script>
</body>
</html>